package com.util;

import java.math.BigDecimal;
import java.math.RoundingMode;

/***
 * 类描述: 定制版 BigDecimal，用于解决精度丢失问题
 *
 * @author Jack
 * @date 23/05/2015
 */
public class MFBigDecimal {

    public BigDecimal val = new BigDecimal("0");

    public MFBigDecimal(Double d) {
        val = new BigDecimal(d == null ? "0" : String.valueOf(d));
    }

    public MFBigDecimal(Integer d) {
        val = new BigDecimal(d == null ? "0" : String.valueOf(d));
    }

    public MFBigDecimal add(Double d) {
        val = val.add(new BigDecimal(d == null ? "0" : String.valueOf(d)));
        return this;
    }

    public MFBigDecimal add(Integer d) {
        val = val.add(new BigDecimal(d == null ? "0" : String.valueOf(d)));
        return this;
    }

    public MFBigDecimal subtract(Double d) {
        val = val.subtract(new BigDecimal(d == null ? "0" : String.valueOf(d)));
        return this;
    }

    public MFBigDecimal multiply(Double d) {
        val = val.multiply(new BigDecimal(d == null ? "0" : String.valueOf(d)));
        return this;
    }

    public MFBigDecimal multiply(Integer d) {
        val = val.multiply(new BigDecimal(d == null ? "0" : String.valueOf(d)));
        return this;
    }

    public MFBigDecimal divide(Double d) {
        val = val.divide(new BigDecimal(String.valueOf(d)), 6, RoundingMode.DOWN);
        return this;
    }

    public MFBigDecimal divide(Integer d) {
        val = val.divide(new BigDecimal(String.valueOf(d)), 6, RoundingMode.DOWN);
        return this;
    }

    public Double doubleValue(int scale, RoundingMode mode) {
        return val.setScale(scale, mode).doubleValue();
    }

}
